{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.23 \u5728\u5c40\u90e8\u53d8\u91cf\u57df\u4e2d\u6267\u884c\u4ee3\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u4f7f\u7528\u8303\u56f4\u5185\u6267\u884c\u67d0\u4e2a\u4ee3\u7801\u7247\u6bb5\uff0c\u5e76\u4e14\u5e0c\u671b\u5728\u6267\u884c\u540e\u6240\u6709\u7684\u7ed3\u679c\u90fd\u4e0d\u53ef\u89c1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5148\u8bd5\u8bd5\u4e00\u4e2a\u7b80\u5355\u573a\u666f\u3002\u9996\u5148\uff0c\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u5185\u6267\u884c\u4e00\u4e2a\u4ee3\u7801\u7247\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = 13\nexec('b = a + 1')\nprint(b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\uff0c\u518d\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u6267\u884c\u540c\u6837\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def test():\n    a = 13\n    exec('b = a + 1')\n    print(b)\ntest()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u540e\u629b\u51fa\u4e86\u4e00\u4e2aNameError\u5f02\u5e38\uff0c\u5c31\u8ddf\u5728 exec() \u8bed\u53e5\u4ece\u6ca1\u6267\u884c\u8fc7\u4e00\u6837\u3002\n\u8981\u662f\u4f60\u60f3\u5728\u540e\u9762\u7684\u8ba1\u7b97\u4e2d\u4f7f\u7528\u5230 exec() \u6267\u884c\u7ed3\u679c\u7684\u8bdd\u5c31\u4f1a\u6709\u95ee\u9898\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4fee\u6b63\u8fd9\u6837\u7684\u9519\u8bef\uff0c\u4f60\u9700\u8981\u5728\u8c03\u7528 exec() \u4e4b\u524d\u4f7f\u7528 locals() \u51fd\u6570\u6765\u5f97\u5230\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u5b57\u5178\u3002\n\u4e4b\u540e\u4f60\u5c31\u80fd\u4ece\u5c40\u90e8\u5b57\u5178\u4e2d\u83b7\u53d6\u4fee\u6539\u8fc7\u540e\u7684\u53d8\u91cf\u503c\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def test():\n    a = 13\n    loc = locals()\n    exec('b = a + 1')\n    b = loc['b']\n    print(b)\ntest()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u4e0a\u5bf9\u4e8e exec() \u7684\u6b63\u786e\u4f7f\u7528\u662f\u6bd4\u8f83\u96be\u7684\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u5f53\u4f60\u8981\u8003\u8651\u4f7f\u7528 exec() \u7684\u65f6\u5019\uff0c\n\u8fd8\u6709\u53e6\u5916\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff08\u6bd4\u5982\u88c5\u9970\u5668\u3001\u95ed\u5305\u3001\u5143\u7c7b\u7b49\u7b49\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u800c\uff0c\u5982\u679c\u4f60\u4ecd\u7136\u8981\u4f7f\u7528 exec() \uff0c\u672c\u8282\u5217\u51fa\u4e86\u4e00\u4e9b\u5982\u4f55\u6b63\u786e\u4f7f\u7528\u5b83\u7684\u65b9\u6cd5\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cexec() \u4f1a\u5728\u8c03\u7528\u8005\u5c40\u90e8\u548c\u5168\u5c40\u8303\u56f4\u5185\u6267\u884c\u4ee3\u7801\u3002\u7136\u800c\uff0c\u5728\u51fd\u6570\u91cc\u9762\uff0c\n\u4f20\u9012\u7ed9 exec() \u7684\u5c40\u90e8\u8303\u56f4\u662f\u62f7\u8d1d\u5b9e\u9645\u5c40\u90e8\u53d8\u91cf\u7ec4\u6210\u7684\u4e00\u4e2a\u5b57\u5178\u3002\n\u56e0\u6b64\uff0c\u5982\u679c exec() \u5982\u679c\u6267\u884c\u4e86\u4fee\u6539\u64cd\u4f5c\uff0c\u8fd9\u79cd\u4fee\u6539\u540e\u7684\u7ed3\u679c\u5bf9\u5b9e\u9645\u5c40\u90e8\u53d8\u91cf\u503c\u662f\u6ca1\u6709\u5f71\u54cd\u7684\u3002\n\u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2a\u6f14\u793a\u5b83\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def test1():\n    x = 0\n    exec('x += 1')\n    print(x)\ntest1()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u4ee3\u7801\u91cc\uff0c\u5f53\u4f60\u8c03\u7528 locals() \u83b7\u53d6\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u4f60\u83b7\u5f97\u7684\u662f\u4f20\u9012\u7ed9 exec() \u7684\u5c40\u90e8\u53d8\u91cf\u7684\u4e00\u4e2a\u62f7\u8d1d\u3002\n\u901a\u8fc7\u5728\u4ee3\u7801\u6267\u884c\u540e\u5ba1\u67e5\u8fd9\u4e2a\u5b57\u5178\u7684\u503c\uff0c\u90a3\u5c31\u80fd\u83b7\u53d6\u4fee\u6539\u540e\u7684\u503c\u4e86\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6f14\u793a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def test2():\n    x = 0\n    loc = locals()\n    print('before:', loc)\n    exec('x += 1')\n    print('after:', loc)\n    print('x =', x)\ntest2()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ed4\u7ec6\u89c2\u5bdf\u6700\u540e\u4e00\u6b65\u7684\u8f93\u51fa\uff0c\u9664\u975e\u4f60\u5c06 loc \u4e2d\u88ab\u4fee\u6539\u540e\u7684\u503c\u624b\u52a8\u8d4b\u503c\u7ed9x\uff0c\u5426\u5219x\u53d8\u91cf\u503c\u662f\u4e0d\u4f1a\u53d8\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f7f\u7528 locals() \u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u6ce8\u610f\u64cd\u4f5c\u987a\u5e8f\u3002\u6bcf\u6b21\u5b83\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\nlocals() \u4f1a\u83b7\u53d6\u5c40\u90e8\u53d8\u91cf\u503c\u4e2d\u7684\u503c\u5e76\u8986\u76d6\u5b57\u5178\u4e2d\u76f8\u5e94\u7684\u53d8\u91cf\u3002\n\u8bf7\u6ce8\u610f\u89c2\u5bdf\u4e0b\u4e0b\u9762\u8fd9\u4e2a\u8bd5\u9a8c\u7684\u8f93\u51fa\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def test3():\n    x = 0\n    loc = locals()\n    print(loc)\n    exec('x += 1')\n    print(loc)\n    locals()\n    print(loc)\ntest3()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u6700\u540e\u4e00\u6b21\u8c03\u7528 locals() \u7684\u65f6\u5019x\u7684\u503c\u662f\u5982\u4f55\u88ab\u8986\u76d6\u6389\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a locals() \u7684\u4e00\u4e2a\u66ff\u4ee3\u65b9\u6848\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f60\u81ea\u5df1\u7684\u5b57\u5178\uff0c\u5e76\u5c06\u5b83\u4f20\u9012\u7ed9 exec() \u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def test4():\n    a = 13\n    loc = { 'a' : a }\n    glb = { }\n    exec('b = a + 1', glb, loc)\n    b = loc['b']\n    print(b)\ntest4()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 exec() \u7684\u6700\u4f73\u5b9e\u8df5\u3002\n\u4f60\u53ea\u9700\u8981\u4fdd\u8bc1\u5168\u5c40\u548c\u5c40\u90e8\u5b57\u5178\u5728\u540e\u9762\u4ee3\u7801\u8bbf\u95ee\u65f6\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\uff0c\u5728\u4f7f\u7528 exec() \u4e4b\u524d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u95ee\u4e0b\u81ea\u5df1\u662f\u5426\u6709\u5176\u4ed6\u66f4\u597d\u7684\u66ff\u4ee3\u65b9\u6848\u3002\n\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u5f53\u4f60\u8981\u8003\u8651\u4f7f\u7528 exec() \u7684\u65f6\u5019\uff0c\n\u8fd8\u6709\u53e6\u5916\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u5982\u88c5\u9970\u5668\u3001\u95ed\u5305\u3001\u5143\u7c7b\uff0c\u6216\u5176\u4ed6\u4e00\u4e9b\u5143\u7f16\u7a0b\u7279\u6027\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}